﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{
    /*************************************************************************************
 *
 * 文 件 名:   Project
 * 描    述: 
 * 
 * 版    本：  V1.0
 * 创 建 者：  Fly
 * 创建时间：  2025/9/7 14:15:21
 *
 * --微信--：  xff19880420
 * --Email-：  xff2006@163.com
 * ======================================
 * 历史更新记录
 * 版本：V          修改时间：         修改人：
 * 修改内容：
 * ======================================
*************************************************************************************/
    public class Project
    {
        ///<summary> 模块列表</summary>
        public List<ModuleObjBase> ModuleObjList { get; set; } = new List<ModuleObjBase>();//模块列表
        ///<summary>模块信息列表</summary>
        public List<ModuleParam> ModuleInfo { get; set; } = new List<ModuleParam>();
        /// <summary>模块修改事件 </summary>
        public event EventHandler<EventArgs> mChangeEvent;
        /// <summary>触发模块事件 </summary>
        public void ChangeEvent()
        {
            mChangeEvent?.Invoke(this, new EventArgs());
        }
        //public List<ModuleParam> mModInfoDic = new List<ModuleParam>();

        /// <summary>修改位置</summary>
        /// <param name="ChangName">模块名称</param>
        /// <param name="GoalName">目标位置的模块名称</param>
        /// <param name="isNext">是否在目标下方追加</param>
        public void ChangePos(string ChangName, string GoalName, bool isNext, ModFlowItem itemA, ModFlowItem itemB)
        {
            try
            {
                ModuleObjBase ModInfo = ModuleObjList.First(x => x.ModuleParam.ModuleName == ChangName);
                string PluginName = "";
                if (ModInfo.ModuleParam.PluginName == "如果" ||
                   ModInfo.ModuleParam.PluginName == "否则" ||
                   ModInfo.ModuleParam.PluginName == "否则如果" ||
                   ModInfo.ModuleParam.PluginName == "结束")
                {
                    PluginName = "条件分支";
                }
                else
                {
                    PluginName = ModInfo.ModuleParam.PluginName;
                }
                if (PluginName == "条件分支" && !ModInfo.ModuleParam.ModuleName.StartsWith("如果"))// 
                {
                    return;
                }
                if (ModInfo.ModuleParam.ModuleName.StartsWith("结束"))
                {
                    return;
                }
                if (ModInfo.ModuleParam.ModuleName.StartsWith("文件夹结束"))
                {
                    return;
                }
                if (ModInfo.ModuleParam.ModuleName.StartsWith("坐标补正结束"))
                {
                    return;
                }

                int NewPos = ModuleObjList.FindIndex(c => c.ModuleParam.ModuleName == GoalName);
                int OldPos = ModuleObjList.FindIndex(c => c.ModuleParam.ModuleName == ChangName);
                //批量移动
                if (ChangName.StartsWith("坐标补正") || ChangName.StartsWith("文件夹") || ChangName.StartsWith("如果") || ChangName.StartsWith("循环开始"))
                {
                    string r = "";
                    int num2 = -1;
                    #region 模块文件夹 判断区域
                    if (ChangName.StartsWith("坐标补正")) //2022-09-29  fly 加
                    {
                        r = "坐标补正结束" + ChangName.Replace("坐标补正", "");
                        num2 = ModuleObjList.FindIndex(x => x.ModuleParam.ModuleName == r);
                    }

                    if (ChangName.StartsWith("文件夹")) //2022-09-29  fly 加
                    {
                        r = "文件夹结束" + ChangName.Replace("文件夹", "");
                        num2 = ModuleObjList.FindIndex(x => x.ModuleParam.ModuleName == r);
                    }

                    if (ChangName.StartsWith("如果")) //2022-09-29  fly 加
                    {
                        r = "结束" + ChangName.Replace("如果", "");
                        num2 = ModuleObjList.FindIndex(c => c.ModuleParam.ModuleName == r);
                    }

                    if (ChangName.StartsWith("循环开始")) //2022-09-29  fly 加
                    {
                        r = "循环结束" + ChangName.Replace("循环开始", "");
                        num2 = ModuleObjList.FindIndex(c => c.ModuleParam.ModuleName == r);
                    }

                    if (OldPos < NewPos && num2 > NewPos) //2022-09-29  fly 加
                    {
                        return;
                    }
                    #endregion
                    if (OldPos < NewPos) //模块往下拖
                    {
                        int rr = 1;
                        for (int i = OldPos; i < num2 + 1; i++)
                        {
                            ModuleObjBase t = ModuleObjList[i];
                            ModuleObjList.Insert(NewPos + rr, t);
                            rr++;
                        }
                        for (int i = OldPos; i < num2 + 1; i++)
                        {
                            ModuleObjList.RemoveAt(OldPos);
                        }
                    }
                    else //模块往上拖
                    {
                        int rr = 0; //起始值
                        List<ModuleObjBase> list1 = new List<ModuleObjBase>();
                        for (int i = OldPos; i < num2 + 1; i++)
                        {
                            list1.Add(ModuleObjList[i]);
                        }
                        foreach (var item in list1)
                        {
                            ModuleObjList.Remove(item);
                        }
                        foreach (var item in list1)
                        {
                            ModuleObjList.Insert(NewPos + rr, item);
                            rr++;
                        }
                    }
                }
                else
                {
                    NewPos = NewPos < OldPos ? ++NewPos : NewPos;
                    ModuleObjList.Remove(ModInfo); //ToDo:模块位置变更-原位删除
                    ModuleObjList.Insert(NewPos, ModInfo);//ToDo:模块位置变更-新位置插入
                    //if (NewPos < OldPos) // 往上拖
                    //{
                    //    NewPos = NewPos + 1;
                    //    ModuleObjList.Remove(ModInfo);
                    //    ModuleObjList.Insert(NewPos, ModInfo);//
                    //} 
                    //else //往下拖
                    //{
                    //    ModuleObjList.Remove(ModInfo);//ToDo:模块位置变更-原位删除
                    //    ModuleObjList.Insert(NewPos, ModInfo);//ToDo:模块位置变更-新位置插入
                    //}
                }
                GetInfoList();
                ChangeEvent();
            }
            catch { }
        }
        ///<summary>获取模块信息列表</summary>
        public void GetInfoList()
        {
            if (ModuleInfo == null)
            {
                ModuleInfo = new List<ModuleParam>();
            }
            ModuleInfo.Clear();
            foreach (ModuleObjBase objBase in ModuleObjList)
            {
                ModuleInfo.Add(objBase.ModuleParam);
            }
        }
        /// <summary>添加一个模块</summary>
        /// <param name="ChangMod">要追加的模块目标位置模块名称</param>
        /// <param name="mModParam">模块信息</param>
        /// <param name="isNext">是否在后方追加</param>
        public void AddModObj(string ChangName, ModuleParam mModParam, bool isNext)
        {
            int index = ModuleObjList.FindIndex(x => x.ModuleParam.ModuleName == ChangName) + 1;
            if (index==0)
            {
                index = ModuleObjList.Count();//添加到最后
            }
            ModuleObjBase NewObjBase = new ModuleObjBase();
            NewObjBase.ModuleParam = mModParam;
            AddModObj(NewObjBase, index);
            if (mModParam.PluginName == "文件夹")
            {
                NewObjBase = new ModuleObjBase();
                ModuleParam moduleParam = new ModuleParam();
                moduleParam.PluginName = "文件夹结束";
                moduleParam.ModuleName = "文件夹结束";
                moduleParam.WindowName = "win3";
                moduleParam.CostTime = 0;
                string path = "E:\\ai lfy\\TreeExHZL\\WindowsFormsApp1\\Resource\\文件夹1.ico";
                Icon icon = new Icon(path);
                moduleParam.Icon = icon;
                NewObjBase.ModuleParam = moduleParam;
                AddModObj(NewObjBase, index+1);
            }
            if (mModParam.PluginName == "坐标补正")
            {
                NewObjBase = new ModuleObjBase();
                ModuleParam moduleParam = new ModuleParam();
                moduleParam.PluginName = "坐标补正结束";
                moduleParam.ModuleName = "坐标补正结束";
                moduleParam.WindowName = "win3";
                moduleParam.CostTime = 0;
                string path = "E:\\ai lfy\\TreeExHZL\\WindowsFormsApp1\\Resource\\坐标系.ico";
                Icon icon = new Icon(path);
                moduleParam.Icon = icon;
                NewObjBase.ModuleParam = moduleParam;
                AddModObj(NewObjBase, index + 1);
            }

            if (mModParam.PluginName == "如果")
            {
                NewObjBase = new ModuleObjBase();
                ModuleParam moduleParam = new ModuleParam();
                moduleParam.PluginName = "结束";
                moduleParam.ModuleName = "结束";
                moduleParam.ModuleRemarks = "双击添加分支条件";
                moduleParam.WindowName = "win3";
                moduleParam.CostTime = 0;
                string path = "E:\\ai lfy\\TreeExHZL\\WindowsFormsApp1\\Resource\\end.ico";
                Icon icon = new Icon(path);
                moduleParam.Icon = icon;
                NewObjBase.ModuleParam = moduleParam;
                AddModObj(NewObjBase, index + 1);
            }
            GetInfoList();
            ChangeEvent();
        }
        ///<summary>增加新的模块到流程列表</summary>
        public void AddModObj(ModuleObjBase objBase, int index)
        {
            ModuleObjBase moduleObjBase;
            moduleObjBase = objBase;
            if (ModuleObjList != null)
            {
                if (ModuleObjList.Count > 0)
                {
                    moduleObjBase = ModuleObjList.First();
                }
            }
            int EncodeMax = 0; //确定新模块的不重名 名称
            List<string> nameList = ModuleObjList.Select(c => c.ModuleParam.ModuleName).ToList();
            while (true)
            {
                if (!nameList.Contains(objBase.ModuleParam.PluginName + EncodeMax))
                {
                    break;//没有重名就跳出循环
                }
                EncodeMax++;
            }
            objBase.ModuleParam.WindowName = moduleObjBase.ModuleParam.WindowName;
            objBase.ModuleParam.ModuleEncode = EncodeMax;
            objBase.ModuleParam.ModuleName = objBase.ModuleParam.PluginName + objBase.ModuleParam.ModuleEncode;
            objBase.ModuleParam.ProjectID = 1; //唯一 流程id
            objBase.ModuleParam.ProjectName = "ProjectInfo.ProjectName";
            objBase.ModuleParam.State = ModState.None;
            if (index > -1)
            {
                //if (objBase.ModuleParam.PluginName == "否则如果")
                //{
                //    for (int i = index; i > 0; i--) //向前找对应索引（否则如果）插入（否则）前面 2022/10/15 fly 上午加
                //    {
                //        if (ModuleObjList[i].ModuleParam.PluginName == "否则")
                //        {
                //            index = i; //否则的索引
                //            break;
                //        }
                //    }
                //    ModuleObjList.Insert(index, objBase); //否则如果 插入 否则 前面
                //}
                //else
                    ModuleObjList.Insert(index, objBase); //其它工具插入
            }
        }
    }
}
